package com.grm.sys.service.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.grm.sys.entity.SysMenu;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 系统菜单映射器
 *
 * @author gaorimao
 * @date 2022/06/24
 */
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    /**
     * 通过用户id查询菜单
     *
     * @param userId 用户id
     * @return {@link List}<{@link SysMenu}>
     */
    @Select("<script> " +
            "select " +
            "    distinct sm.* " +
            "from " +
            "    sys_menu sm " +
            "left join sys_role_menu srm " +
            "on " +
            "    sm.id = srm.menu_id " +
            "left join sys_role sr " +
            "on " +
            "    srm.role_id = sr.id " +
            "left join sys_user_role sur " +
            "on " +
            "    sr.id = sur.role_id " +
            "left join sys_user su  " +
            "on " +
            "    sur.user_id = su.id " +
            "<where> " +
            "    <if test = 'userId != null'> " +
            "    and su.id = #{userId} " +
            "    </if> " +
            "</where> " +
            "group by parent_id,id order by parent_id ,order_num " +
            "</script> ")
    List<SysMenu> queryMenusByUserId(Long userId);

    /**
     * 通过用户id查询菜单ids
     *
     * @param userId 用户id
     * @return {@link List}<{@link Long}>
     */
    @Select("SELECT DISTINCT srm.menu_id " +
            "FROM sys_user_role sur LEFT JOIN sys_role_menu srm " +
            "ON sur.role_id = srm.role_id " +
            "WHERE sur.user_id = #{userId} ")
    List<Long> queryMenuIdsByUserId(Long userId);
}
